﻿/*
 * Ext Gantt 1.2
 * Copyright(c) 2009-2011 Mats Bryntse Consulting
 * mats@ext-scheduler.com
 * http://www.ext-scheduler.com/license.html
 *
 */

Ext.ns('Sch.gantt');
 
/**
 * @class Sch.gantt.Tooltip
 * @extends Ext.ToolTip
 * Internal plugin showing task start/end information.
 * @constructor
 * @param {Object} config The object containing the configuration of this model.
 */
Sch.gantt.Tooltip = function(config, grid) {
    Ext.apply(this, config);
    this.grid = grid;
    Sch.gantt.Tooltip.superclass.constructor.call(this);
};
 
Ext.extend(Sch.gantt.Tooltip, Ext.ToolTip, {
    
    /**
     * @cfg {String} startText The text to show before the start date. Defaults to 'Starts:'.
     */
    startText : 'Starts: ',
    
    /**
     * @cfg {String} endText The text to show before the end date. Defaults to 'Ends:'.
     */
    endText : 'Ends: ',
    
    /**
     * @cfg {String} durationText The text to show before the duration text during a resize operation. Defaults to 'Duration:'.
     */
    durationText : 'Duration:',
    
    /**
     * @cfg {String} dayText The text to show after the duration text during a resize operation. Defaults to 'd'.
     */
    dayText : 'd',
    
    /**
     * @cfg {String} mode "startend" or "duration"
     */
    mode : 'startend', 

    initComponent : function() {    
       
        if (this.mode === 'startend' && !this.startEndTemplate) {
            this.startEndTemplate = new Ext.Template(
                '<div class="sch-timetipwrap {cls}">' +
                '<div>' +
                    this.startText + '{startText}' +
                '</div>' +
                '<div>' +
                    this.endText + '{endText}' +
                '</div>' +
            '</div>'
            ).compile();
        }

        if (this.mode === 'duration' && !this.durationTemplate) {
            this.durationTemplate = new Ext.Template(
                '<div class="sch-timetipwrap {cls}">',
                    '<div>' + this.startText + ' {startText}</div>',
                    '<div>' + this.durationText + ' {duration} ' + this.dayText + '</div>',
                '</div>'
            ).compile();
        }
        
        Sch.gantt.Tooltip.superclass.initComponent.apply(this, arguments);
    },
    
    cls : 'sch-tip',
    width: 145,
    height:40,
    autoHide : false,
    anchor : 'b-tl',

    update : function(start, end, valid) {
        var content;
        if (this.mode === 'duration') {
            content = this.getDurationContent(start, end, valid);
        } else {
            content = this.getStartEndContent(start, end, valid);
        }
        Sch.gantt.Tooltip.superclass.update.call(this, content);
    },
     
    // private
    getStartEndContent : function(start, end, valid) {
        var g = this.grid,
            startText = g.getFormattedDate(start),
            roundedEnd,
            endText = startText;
        
        if (end - start > 0) {
            endText = g.getFormattedEndDate(end, start);
        }
        
        var retVal = {
            cls : valid ? 'sch-tip-ok' : 'sch-tip-notok',
            startText : startText,
            endText : endText
        };
        
        if (this.showClock) {
            Ext.apply(retVal, {
                startHourDegrees : roundedStart.getHours() * 30, 
                startMinuteDegrees : roundedStart.getMinutes() * 6
            });
            
            if (end) {
                Ext.apply(retVal, {
                    endHourDegrees : roundedEnd.getHours() * 30, 
                    endMinuteDegrees : roundedEnd.getMinutes() * 6
                });
            }
        }
        return this.startEndTemplate.apply(retVal);
    },
    
     getDurationContent : function(start, end, valid) {
        var g = this.grid,
            formattedStart = g.getFormattedDate(start),
            duration = g.getDuration(start, end);
        
        return this.durationTemplate.apply({
            cls : valid ? 'sch-tip-ok' : 'sch-tip-notok',
            startText : formattedStart,
            duration : duration
        });
    },

    show : function(el) {
        this.initTarget(el);

        // Rendering is weird if the initial tooltip is empty, prepopulate it with some dummy html
        if (!this.rendered) {
            this.render(Ext.getBody());
            var dummyDate = new Date();
            this.html = this.update(dummyDate, dummyDate, true);
        }
        Sch.gantt.Tooltip.superclass.show.call(this);
    }
}); 
